package com.meichis.yndms.ui.activity;

import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.MinStatisticalAdapter;
import com.meichis.yndms.databinding.ActivityMinprogramStatisticalBinding;
import com.meichis.yndms.hybrid.CallbackContext;
import com.meichis.yndms.hybrid.HybridInterface;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.MinStatisticalVm;

import java.util.ArrayList;

public class MinProgramStatisticalActivity extends BaseActivity implements HybridInterface {

    private ActivityMinprogramStatisticalBinding binding;

    private MinStatisticalVm vm;
    private MinStatisticalAdapter adapter;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_minprogram_statistical);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MinStatisticalVm.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("门店统计");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        adapter = new MinStatisticalAdapter(this, R.layout.item_min_statistical, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);
        addObserver();

        vm.getRegistSPCount();
    }

    private void addObserver() {

        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getMinStatisticalList().observe(this, minStatisticals -> {
            if (minStatisticals.size() == 0) {
                binding.llTitle.setVisibility(View.GONE);
            } else {
                binding.llTitle.setVisibility(View.VISIBLE);
            }

            adapter.setList(minStatisticals);

        });
    }

    @Override
    public void execute(int CallType, String ControlID, Object Revalue, String FUNCCallBack) {

    }

    @Override
    public void setActivityResultCallback(CallbackContext callback) {

    }

    public class ClickEvent {

        public void onSelectDateType() {
            new DropDownDialog<>(MinProgramStatisticalActivity.this, R.color.theme, "时间维度", "Name", vm.getDataTableList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getDataTable().setValue(vm.getDataTableList().get(position));
                    vm.getRegistSPCount();
                }
            }).show();
        }
    }
}
